package com.xuhnagchen.automation.util;


import com.xuhnagchen.automation.email.CaptchaUtils;
import com.xuhnagchen.automation.email.EmailMailInfo;
import com.xuhnagchen.automation.email.EmailSendUtil;
import com.xuhnagchen.automation.email.EmailTemplet;

import javax.servlet.http.HttpSession;


/**
 * ClassName: EMailUtil.java <br>
 * Description: <br>
 * Create by: name：yuxin <br>email: yuruixin_china@163.com <br>
 * Create Time: 2017年6月15日<br>
 */
public class EMailUtil {

    /**
     * Description：发送注册邮件
     *
     * @param email
     * @param captcha
     * @return String
     * @author name：yuxin <br>email: yuruixin_china@163.com
     **/
    public static String sendForReg(String userName, String email, String captcha) throws Exception {
        //session.setAttribute("reg:"+email, captcha);
        String title = "LookFotoe 注册验证码";
        String content = EmailTemplet.getTemplet().getHtml(title, email, userName, "注册", captcha);
        EmailMailInfo info = new EmailMailInfo();
        info.setToAddress(email);
        info.setSubject(title);
        info.setContent(content);

        // MailSendUtil.sendTextMail(info);
        EmailSendUtil.sendHtmlMail(info);

        return captcha;
    }

    /**
     * Description：发送密码重置邮件
     *
     * @param email
     * @return String
     * @author name：yuxin <br>email: yuruixin_china@163.com
     **/
    public static String sendForResetPassword(String userName, String email, String captcha) {
        String title = "LookFotoe  密码重置验证码";
        String content = EmailTemplet.getTemplet().getHtml(title, email, userName, "密码重置", captcha);
        EmailMailInfo info = new EmailMailInfo();
        info.setToAddress(email);
        info.setSubject(title);
        info.setContent(content);
        try {
            // MailSendUtil.sendTextMail(info);
            EmailSendUtil.sendHtmlMail(info);
        } catch (Exception e) {
            System.out.print("'" + title + "'的邮件发送失败！");
            e.printStackTrace();
        }
        return captcha;
    }

    /**
     * Description：校验验证码
     *
     * @param email
     * @param captcha
     * @param session
     * @return boolean
     * @author name：yuxin <br>email: yuruixin_china@163.com
     **/
    public static boolean checkCaptcha(String email, String type, String captcha, HttpSession session) {
        String captchaCache = null;
        switch (type) {
            case "reg":
                captchaCache = (String) session.getAttribute("reg:" + email);
                break;
            case "restPassword":
                captchaCache = (String) session.getAttribute("restPassword:" + email);
                break;
            default:
                break;
        }
        if (captchaCache != null) {
            return captchaCache.equals(captcha);
        }
        return false;
    }

    public static void main(String[] args) throws Exception {
        String captcha = CaptchaUtils.creatCaptcha();
        //注册验证码
        EMailUtil.sendForReg("156856278954", "948340983@qq.com", captcha);
    }
}
